<script type="text/javascript" src="mootools.js"></script>
<script>
// VideoKit accessors that the popup uses.
var selectedId = null;
var tabDB = {};
var selectedVideoList = null;

/*
var VideoInfo = new Class({
	initialize: function(videoSource, json) {
		this.videoSource = videoSource;
		if (videoSource === "youtube") {
			this.videoId = json.entry.media$group.yt$videoid.$t;
			this.videoURL = ;
			this.videoTitle = json.entry.title.$t;
			this.videoDescription = json.entry.media$group.media$description.$t;
			this.videoThumbnail = json.entry.media$group.media$thumbnail[0].url;
			this.videoViewCount = json.entry.yt$statistics.viewCount;
			this.videoDuration = ;
		} else if (videoSource === "vimeo") {
			this.videoId = json[0].id;
			this.videoURL = json[0].url;
			this.videoTitle = json[0].title;
			this.videoDescription = json[0].description;
			this.videoThumbnail = json[0].thumbnail_medium;
			this.videoViewCount = json[0].stats_number_of_plays;
			this.videoDuration = ;
		}
	}
});
*/

// Mootools class that contains the video info list (json) for a tab
var TabVideoList = new Class({
	initialize: function(videoString) {
		var splitted = videoString.split(":");
		var cleanedSplitted = [];
		for (var i=0; i<splitted.length-1; i++) {
			cleanedSplitted.include(splitted[i]);
		}
		this.videoCount = cleanedSplitted.length;
		this.videoList = [];
		for (var i=0; i<cleanedSplitted.length; i++) {
			loadVideoInfo(cleanedSplitted[i], this);
		}
	},
	addVideoInfo: function(videoInfo) {
		this.videoList.include(videoInfo);
	},
	getVideoInfoList: function() {
		return this.videoList;
	},
	isLoaded: function() {
		if (this.videoCount === this.videoList.length) {
			return true;
		} else {
			return false;
		}
	}
});

function loadVideoInfo(videoId, tabVideoList) {
	var jsonRequest = new Request.JSON(
		{
			url: "http://gdata.youtube.com/feeds/api/videos/" + videoId + "?v=2&alt=json",
			onSuccess: function(videoInfo) {
	    		tabVideoList.addVideoInfo(videoInfo);
			}
		}
	).get();
}

function updateVideos(tabId) {
	chrome.tabs.sendRequest(tabId, {}, function(videos) {
		//if (!videos) {
		if (videos.length === 0) {
			chrome.pageAction.hide(tabId);
		} else {
			chrome.pageAction.show(tabId);
			tabDB[tabId] = new TabVideoList(videos);
			if (selectedId == tabId) {
				updateSelected(tabId);
			}
		}
	});
}

function updateSelected(tabId) {
	selectedVideoList = tabDB[tabId];
	if (selectedVideoList)
		chrome.pageAction.setTitle({tabId:tabId, title:"videos in this page"});
}

chrome.tabs.onUpdated.addListener(function(tabId, change, tab) {
	if (change.status == "complete") {
		updateVideos(tabId);
	}
});

chrome.tabs.onSelectionChanged.addListener(function(tabId, info) {
	selectedId = tabId;
	updateSelected(tabId);
});

// Ensure the current selected tab is set up.
chrome.tabs.getSelected(null, function(tab) {
	updateVideos(tab.id);
});
</script>
